#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include<sys/time.h>
#include<fcntl.h>
#include<unistd.h> 
#include<pthread.h>

void* myfunc(void* p){
	for(int i = 0; i < 5; i ++){
		printf("pthread: this is %dth printf\n", i);
	}
	pthread_exit(NULL);
}

int main(int argc, char* args[])
{
	int ret;
	pthread_t tid;
	pthread_attr_t attr;
	ret = pthread_attr_init(&attr);
	if(ret){
		fprintf(stderr,"pthread_attr_init error: %s\n", strerror(ret)); 
		exit(1);
	}
	ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	
	if(ret){
		fprintf(stderr,"pthread_attr_setdetachstate error: %s\n", strerror(ret));
		exit(1);
	}

	ret = pthread_create(&tid, &attr, myfunc, NULL);
	if(ret){
		fprintf(stderr, "pthread_creat error: %s\n", strerror(ret));
		exit(1);
	}

	ret = pthread_attr_destroy(&attr);
	
	ret = pthread_join(tid, NULL);
	if(ret){
		fprintf(stderr, "pthread_join error: %s\n", strerror(ret));
		sleep(5);
		exit(1);
	}
	return 0;	
}
